import time
import schedule
import asyncio
from spider import Spider
from analyzer import Analyzer
from report import ReportGenerator
from mail import MailSender
from summary_analyzer import SummaryAnalyzer
import os
from dotenv import load_dotenv
from tqdm import tqdm
import logging

# 配置日志
logging.basicConfig(
    filename='crawler.log',
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    encoding='utf-8'  # 明确指定UTF-8编码
)
logging.getLogger('urllib3').setLevel(logging.WARNING)  # 减少请求库的日志输出

load_dotenv()

async def job(immediate=False):
    """定时任务主函数"""
    logger = logging.getLogger('main')
    try:
        logger.info('开始执行定时任务')
        # 1. 数据采集
        spider = Spider()
        results = {}
        
        # 微博数据采集与验证
        weibo_data = await spider.crawl_weibo()
        if weibo_data and weibo_data.get('data'):
            results['weibo'] = weibo_data
        
        # 知乎数据采集与验证
        zhihu_data = await spider.crawl_zhihu()
        if zhihu_data and zhihu_data.get('data'):
            results['zhihu'] = zhihu_data
            
        # 抖音数据采集与验证
        douyin_data = await spider.crawl_douyin()
        if douyin_data and douyin_data.get('data'):
            results['douyin'] = douyin_data
            
        # 头条数据采集与验证
        toutiao_data = await spider.crawl_toutiao()
        if toutiao_data and toutiao_data.get('data'):
            results['toutiao'] = toutiao_data

        if not results:
            logger.warning('没有采集到有效数据，跳过报告生成')
            return

        # 2. 数据分析
        analyzer = Analyzer()
        analyses = {}
        for platform, data in results.items():
            try:
                analysis = analyzer.analyze_trends(data)
                if analysis:  # 确保分析结果有效
                    analyses[platform] = analysis
            except Exception as e:
                logger.error(f'{platform}数据分析失败: {e}')

        if not analyses:
            logger.warning('没有生成有效分析结果，跳过报告生成')
            return

        # 3. 报告生成
        report_gen = ReportGenerator()
        report_files = []
        for platform, analysis in analyses.items():
            try:
                report_file = report_gen.generate_docx(analysis, platform)
                if report_file:  # 确保报告文件生成成功
                    report_files.append(report_file)
            except Exception as e:
                logger.error(f'{platform}报告生成失败: {e}')
                
        # 4. 生成综合分析报告
        try:
            logger.info('开始生成综合分析报告')
            summary_analyzer = SummaryAnalyzer(analyzer)
            combined_data = summary_analyzer.collect_platform_data(results)
            if combined_data:
                summary_analysis = summary_analyzer.analyze_summary(combined_data)
                if summary_analysis:
                    summary_report = summary_analyzer.generate_summary_report(summary_analysis)
                    if summary_report:
                        report_files.append(summary_report)
                        logger.info(f'综合分析报告生成成功: {summary_report}')
        except Exception as e:
            logger.error(f'综合分析报告生成失败: {e}')

        # 5. 邮件发送
        if os.getenv('ENABLE_EMAIL', 'false').lower() == 'true' and report_files:
            mail_sender = MailSender()
            try:
                # 直接传递报告文件列表
                success = mail_sender.send_reports(report_files)
                if success:
                    logger.info('邮件发送完成')
                else:
                    logger.error('邮件发送失败')
            except Exception as e:
                logger.error(f'邮件发送失败: {e}')
        else:
            logger.info(f'邮件发送已禁用或没有报告文件: ENABLE_EMAIL={os.getenv("ENABLE_EMAIL", "false")}, 报告文件数量={len(report_files) if report_files else 0}')
            logger.info('没有有效报告可发送')

    except Exception as e:
        logger.error(f'任务执行失败: {str(e)}', exc_info=True)
        print(f'任务执行失败: {e}')
        logging.error(f'任务执行失败: {e}')

def run_job_immediately():
    """立即执行一次任务"""
    asyncio.run(job(immediate=True))

def run_schedule():
    """运行定时任务"""
    # 设置定时任务，每天11:50执行
    schedule_time = os.getenv('SCHEDULE_TIME', '11:50')
    schedule.every().day.at(schedule_time).do(run_job_immediately)
    print(f'已设置定时任务，将在每天 {schedule_time} 执行')
    
    # 如果设置了立即执行，则立即运行一次
    if os.getenv('RUN_IMMEDIATELY', 'false').lower() == 'true':
        print('立即执行一次任务')
        run_job_immediately()
    
    # 循环等待定时任务
    while True:
        schedule.run_pending()
        time.sleep(60)  # 每分钟检查一次是否有待执行的任务

if __name__ == '__main__':
    try:
        run_schedule()  # 启动定时任务
    except KeyboardInterrupt:
        print('\n程序已手动终止')
    except Exception as e:
        print(f'程序异常: {e}')
        logging.error(f'程序异常: {e}')